package com.adeaz.sdk.demo.wow;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.adeaz.AdeazAdListener;
import com.adeaz.banner.AdeazBannerView;
import com.adeaz.sdk.demo.R;
import com.adeaz.sdk.demo.wow.AdData;
import com.bumptech.glide.Glide;

public class BannerAdapter extends PagerAdapter {

    protected static final String TAG = BannerAdapter.class.getSimpleName();
    private List<AdData> adList;
    private LayoutInflater inflater;

    private Context context;

    private HashMap<Integer, View> viewHashMap = new HashMap<>();

    public BannerAdapter(Context context, List<AdData> adList) {
        this.context = context;
        this.adList = adList;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return adList.size();
    }

    /**
     * position 值已被LoopAdapter 处理过，所以不需要在做处理
     */
    @Override
    public Object instantiateItem(ViewGroup view, int position) {

        View childView = viewHashMap.get(position);
        if (childView == null) {
            childView = createView(position);
            viewHashMap.put(position, childView);
        }
        ViewGroup v = (ViewGroup) childView.getParent();
        if (v != null) {
            v.removeView(childView);
        }
        view.addView(childView);
        return childView;
    }

    private View createView(int position) {
        View childView = LayoutInflater.from(context).inflate(R.layout.item_pager_image, null);
        ImageView imageView = (ImageView) childView.findViewById(R.id.image);
        RelativeLayout layout = (RelativeLayout) childView.findViewById(R.id.banner_relayout);
        final AdData adData = adList.get(position);
        TextView con = (TextView) childView.findViewById(R.id.content);
        con.setText("position:" + position);
//        imageView.setVisibility(View.GONE);
//        layout.setVisibility(View.VISIBLE);
//
//        layout.removeAllViews();
//        layout.postInvalidate();
//        AdeazBannerView banner = new AdeazBannerView(layout, "1021895");
//        banner.setAdListener(listener);
//        banner.loadAds();
        Glide.with(context).load(adData.url).into(imageView);


//
//    // 设置点击事件
//    childView.setOnClickListener(new
//
//    OnClickListener() {
//        @Override
//        public void onClick (View v){
//
//            if (adData.source == 2) {
//
//            } else {
//
//                Log.d(TAG, "click:" + adData.click);
//                // 上传友盟统计广告点击事件
//                UmengUtils.reportEvent(context, adData.click);
//                MainModuleFactory.toAdWebPage(context, adData);
//            }
//        }
//    }
//
//    );
        return childView;
    }

    AdeazBannerView banner = null;
    AdeazAdListener listener = new AdeazAdListener() {

        @Override
        public void onNoAd() {
            Log.e(TAG, "onNoAd");
        }

        @Override
        public void onAdTimeout() {
            Log.e(TAG, "onAdTimeout");
        }

        @Override
        public void onAdPlay() {
            Log.e(TAG, "onAdPlay");

        }

        @Override
        public void onAdOver() {
            Log.e(TAG, "onAdOver");
        }

        @Override
        public void onAdClosed() {
            Log.e(TAG, "onAdClosed");
        }

        @Override
        public void onAdClicked() {
            Log.e(TAG, "onAdClicked");
        }

        @Override
        public void onLoadAdFailed() {
            Log.e(TAG, "onLoadAdFailed");
        }

        @Override
        public void onAdExposured() {
            // TODO Auto-generated method stub
            Log.e(TAG, "onAdExposured");
        }
    };


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}